package skin

import (
	"context"
	"encoding/json"
	"group-card/api/internal/svc"
	"group-card/api/internal/types"
	"group-card/common/response"
	"group-card/rpc/card/cardclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type SkinUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSkinUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SkinUpdateLogic {
	return &SkinUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SkinUpdateLogic) SkinUpdate(req *types.UpdateSkinRequest) (resp *types.SkinResponse, err error) {
	_, err = l.svcCtx.Card.SkinUpdate(l.ctx, &cardclient.SkinUpdateRequest{
		Id:         req.Id,
		Name:       req.Name,
		FaceValue:  req.FaceValue,
		Colour:     req.Colour,
		CustomerId: req.CustomerId,
		Type:       req.Type,
		Status:     req.Status,
		Image:      req.Image,
		AdminId:    req.AdminId,
	})

	if err != nil {
		reqStr, _ := json.Marshal(req)
		logx.WithContext(l.ctx).Errorf("编辑卡面信息失败,参数:%s,异常:%s", reqStr, err.Error())
		return &types.SkinResponse{Code: response.ErrCode, Message: "编辑卡面失败"}, nil
	}

	return &types.SkinResponse{
		Code:    response.SuccessCode,
		Message: "编辑卡面成功",
	}, nil
}
